from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from apps.goods.models import SKUImage, SKU
from apps.meiduo_admin.serializers.images import SKUImageModelSerializer, ImageSKUModelSerializer
from apps.meiduo_admin.utils import PageNum
from rest_framework.pagination import PageNumberPagination
from rest_framework import status



class ImageModelViewSet(ModelViewSet):
    queryset = SKUImage.objects.all()
    serializer_class =SKUImageModelSerializer
    pagination_class = PageNum

    def create(self, request, *args, **kwargs):
        image=request.data.get('image')
        image_data=image.read()
        sku_id=request.data.get('sku')
        try:
            SKU.objects.get(id=sku_id)
        except SKU.DoesNotExist:
            return Response(status=status.HTTP_400_BAD_REQUEST)
        from fdfs_client.client import Fdfs_client
        client=Fdfs_client('utils/fastdfs/client.conf')
        result=client.upload_by_buffer(image_data)
        if result['Status']!='Upload successed.':
            return Response(status=status.HTTP_400_BAD_REQUEST)
        file_id=result.get('Remote file_id')
        new_image=SKUImage.objects.create(
            sku_id=sku_id,
            image=file_id
        )
        return Response({
            'id':new_image.id,
            'sku':sku_id,
            'image':new_image.image.url
        },status=status.HTTP_201_CREATED)

    def update(self, request, *args, **kwargs):
        image_object=self.get_object()
        id=image_object.id
        sku_id=request.data.get('sku')
        image=request.data.get('image')
        try:
            SKU.objects.get(id=sku_id)
        except SKU.DoesNotExist:
            return Response(status=status.HTTP_400_BAD_REQUEST)
        from fdfs_client.client import Fdfs_client
        client=Fdfs_client('utils/fastdfs/client.conf')
        result=client.upload_by_buffer(image.read())
        if result['Status']!='Upload successed.':
            return Response(status=status.HTTP_400_BAD_REQUEST)
        file_id=result['Remote file_id']
        new_skuimage=SKUImage.objects.filter(id=id).update(
            sku=sku_id,
            image=file_id
        )
        return Response({
            'id': id,
            'sku': sku_id,
            'image': image_object.image.url
        }, status=status.HTTP_201_CREATED)
    # def update(self, request, *args, **kwargs):
    #     sku_id=request.data.get('sku')
    #     image=request.data.get('image')
    #     try:
    #         SKU.objects.get(id=sku_id)
    #     except SKU.DoesNotExist:
    #         Response(status=status.HTTP_400_BAD_REQUEST)
    #     from fdfs_client.client import Fdfs_client
    #     client=Fdfs_client('utils/fastdfs/client.conf')
    #     result=client.upload_by_buffer(image.read())
    #     if result['Status']!='Upload successed.':
    #         return Response(status=status.HTTP_400_BAD_REQUEST)
    #     file_id=result['Remote file_id']
    #     imagesku_object=self.get_object()
    #     imagesku_object.sku_id=sku_id
    #     imagesku_object.image=file_id
    #     imagesku_object.save()
    #     return Response({
    #         'id':imagesku_object.id,
    #         'sku':imagesku_object.sku_id,
    #         'image':imagesku_object.image.url
    #     })




from rest_framework.generics import ListAPIView
class ImageListAPIView(ListAPIView):
    queryset = SKU.objects.all()
    serializer_class = ImageSKUModelSerializer